package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.ResponseResult;
import com.demo.domain.entity.LogisticsSend;
import com.demo.domain.vo.LogisticInfovo;
import com.demo.domain.vo.PageVo;
import com.demo.mapper.LogisticsSendMapper;
import com.demo.service.LogisticsSendService;
import com.demo.util.BeanCopyUtils;
import com.demo.util.SecurityUtils;
import org.springframework.stereotype.Service;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



/**
 *
 */
@Service
public class LogisticsSendServiceImpl extends ServiceImpl<LogisticsSendMapper, LogisticsSend>
        implements LogisticsSendService {

    @Override
    public ResponseResult logisticsSend(LogisticsSend logisticsSend) {
        try {
            Date date = new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = dateFormat.parse(dateFormat.format(date));
            logisticsSend.setOrderTime(date1);
        } catch (Exception e) {

        }
        logisticsSend.setAccount(SecurityUtils.getLoginUser().getUsername());
        logisticsSend.setOrderStatus("待支付");
        save(logisticsSend);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult viewOrder(Integer pageNumber) {
        LambdaQueryWrapper<LogisticsSend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LogisticsSend::getAccount, SecurityUtils.getLoginUser().getUsername());
        queryWrapper.orderByDesc(LogisticsSend::getOrderNumber);
        Page<LogisticsSend> page = new Page(pageNumber, 6);
        page(page, queryWrapper);
        List<LogisticsSend> logisticsSends = page.getRecords();
        List<LogisticInfovo> vs = BeanCopyUtils.copyBeanList(logisticsSends, LogisticInfovo.class);
        PageVo pageVo = new PageVo(vs, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }



    @Override
    public ResponseResult LogisticsMap(Integer orderNumber) {
        LambdaQueryWrapper<LogisticsSend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LogisticsSend::getOrderNumber, orderNumber);
        LogisticsSend logisticsSend = getOne(queryWrapper);
        return ResponseResult.okResult(logisticsSend);
    }

    @Override
    public ResponseResult logisticsput(LogisticsSend logisticsSend) {
        UpdateWrapper<LogisticsSend> logisticsSendUpdateWrapper = new UpdateWrapper<>();
        logisticsSendUpdateWrapper.lambda().set(LogisticsSend::getPaymentAmount,logisticsSend.getPaymentAmount())
                .set(LogisticsSend::getOrderStatus,logisticsSend.getOrderStatus())
                .eq(LogisticsSend::getOrderNumber,logisticsSend.getOrderNumber());
        return ResponseResult.okResult(update(logisticsSendUpdateWrapper));
    }

    @Override
    public ResponseResult logisticsinfoput(LogisticsSend logisticsSend) {
        UpdateWrapper<LogisticsSend> logisticsSendUpdateWrapper = new UpdateWrapper<>();
        logisticsSendUpdateWrapper.lambda().set(LogisticsSend::getOrderStatus,logisticsSend.getOrderStatus())
                .eq(LogisticsSend::getOrderNumber,logisticsSend.getOrderNumber());
        update(logisticsSendUpdateWrapper);
        return ResponseResult.okResult();
    }


}
